import 'dart:typed_data';

import 'package:bluetooth_protocols_plugin/library.dart';

import '../error/p6_realtime_error.dart';
import '../status/p6_realtime_status.dart';
import 'p6_realtime_info.dart';

// P6 所有实时信息
class P6RealtimeAllInfo extends LorinRealtimeAllInfo<P6RealtimeInfo,
    P6RealtimeStatus, P6RealtimeError> {
  late Uint8List infoBytes;
  late Uint8List errorBytes;
  late Uint8List statusBytes;

  P6RealtimeAllInfo.fromBytes(Uint8List bytes) : super.fromBytes(bytes) {
    int index = 0;
    infoBytes = bytes.sublist(index, index + 74);
    index += 74;
    statusBytes = bytes.sublist(index, index + 4);
    index += 4;
    errorBytes = bytes.sublist(index, index + 8);
  }

  @override
  P6RealtimeError get error => P6RealtimeError.fromBytes(errorBytes);

  @override
  P6RealtimeInfo get info => P6RealtimeInfo.fromBytes(infoBytes);

  @override
  P6RealtimeStatus get status => P6RealtimeStatus.fromBytes(statusBytes);

  @override
  int dataIntValue() {
    return info.dataIntValue() + error.dataIntValue() + status.dataIntValue();
  }

  @override
  int dataLength() {
    return info.dataLength() + error.dataLength() + status.dataLength();
  }

  @override
  int get minLength => info.minLength + error.minLength + status.minLength;

  @override
  Uint8List toBytes() {
    return Uint8List.fromList(
        [...info.toBytes(), ...error.toBytes(), ...status.toBytes()]);
  }

  @override
  Map<String, dynamic> toMap() {
    return {
      'info': info.toMap(),
      'error': error.toMap(),
      'status': status.toMap(),
      'version': '$runtimeType',
    };
  }
}
